<?php
/*
 * Created on Aug 30, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("../configure/configure.php");		//--> global var
include_once("../admin/configure/admin.config.inc.php");		//--> global var

include_once("../admin/lib/apis/GoogleAffiliateApi.php");
include_once("../lib/constant/GlobalConstant.php");

include_once("../lib/impl/FeedImpl.php");
include_once("impl/ApiLogImpl.php");

include_once("../admin/lib/utils/StringUtils.php");

set_time_limit(7200);

$apiLogImpl	= new ApiLogImpl(DB_TAG_COUPON);
$feedImpl	= new FeedImpl(DB_TAG_COUPON);
$ga 		= new GoogleAffiliateApi();
$merImpl 	= new MerchantImpl(DB_TAG_COUPON);

$arrAdverIds = $apiLogImpl->getRequestAdvertiser(GlobalConstant::SOURCE_ID_GOOGLE);


$ga->init();

echo "start time: " . date('Y-m-d H:m:s');
echo "\n";

echo "new add advertiser count: " . count($arrAdverIds);
echo "\n";

foreach($arrAdverIds as $adverId){
	$createDateMin = date('Y-m-d H:m:s',strtotime("-60 day"));
	
	echo "***************************************************************";
	echo "\n";
	
	echo "adverId: " . $adverId;
	echo "\n";
	
	echo "createDateMin: " . $createDateMin;
	echo "\n";
	
	
///	
	
	$links = $ga->getLinks(100, 5, $createDateMin, "", 'approved', $adverId, '');
	
	
	echo "link count: " . count($links);
	echo "\n";
	
	$newCouponCount 	= 0;
	$newMerchantCount	= 0;
	
	foreach($links as $link){
		
		if(StringUtils::count_words($link->getName()) < 2)// if words than less 2, ignore this coupon
			continue;
		
		$pos = strripos($link->getName(), "logo");
		if($pos > -1){
			continue;
		}
		
		$couponId = $feedImpl->addCoupon($link);
		if($couponId > 0)
			$newCouponCount++;
			
		if($feedImpl->isAddNewMerchant())
			$newMerchantCount++;
			
	}
	
	$oMer 	= $merImpl->getByAdvertiserId(GlobalConstant::SOURCE_ID_GOOGLE, $adverId);
	if(is_object($oMer)){	
		$sql 	= "INSERT INTO log_requested_advertiser SET merchantId = " . $oMer->getId() . ", sourceId = 1, status = 1";
		
		$merImpl->insert(DB_TAG_COUPON, $sql);
	}
	
	echo "new coupon count: " . $newCouponCount;
	echo "\n";
	
	echo "new merchant count: " . $newMerchantCount;
	echo "\n";

	exit;
}



?>
